package com.zishi.venuesbooking.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zishi.venuesbooking.R;
import com.zishi.venuesbooking.entity.Message;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;

public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Message> messages;
    private String currentUserId;

    public MessageAdapter(List<Message> messages, String currentUserId) {
        this.messages = messages;
        this.currentUserId = currentUserId;
    }

    @Override
    public int getItemViewType(int position) {
        return messages.get(position).getType();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == Message.TYPE_SENT) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_sent, parent, false);
            return new SentMessageHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_received, parent, false);
            return new ReceivedMessageHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Message message = messages.get(position);

        if (holder.getItemViewType() == Message.TYPE_SENT) {
            ((SentMessageHolder) holder).bind(message);
        } else {
            ((ReceivedMessageHolder) holder).bind(message);
        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    // 发送消息的ViewHolder
    private static class SentMessageHolder extends RecyclerView.ViewHolder {
        TextView tvContent, tvTime;

        SentMessageHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
        }

        void bind(Message message) {
            tvContent.setText(message.getContent());
            tvTime.setText(formatTime(message.getTimestamp()));
        }
    }

    // 接收消息的ViewHolder
    private static class ReceivedMessageHolder extends RecyclerView.ViewHolder {
        TextView tvContent, tvTime, tvName;
        CircleImageView civAvatar;

        ReceivedMessageHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvName = itemView.findViewById(R.id.tv_sender_name);
            civAvatar = itemView.findViewById(R.id.civ_avatar);
        }

        void bind(Message message) {
            tvContent.setText(message.getContent());
            tvTime.setText(formatTime(message.getTimestamp()));
            tvName.setText(message.getSenderName());

            // 加载头像
            Glide.with(itemView.getContext())
                    .load(message.getAvatarUrl())
                    .placeholder(R.drawable.avata_1)
                    .into(civAvatar);
        }
    }

    private static String formatTime(long timestamp) {
        return new SimpleDateFormat("HH:mm", Locale.getDefault())
                .format(new Date(timestamp));
    }
}
